package controllers

import (
	"gitee.com/tsingeye/FreeEhome/models"
)

/**
 * @apiDefine system 系统接口
 */
type SystemController struct {
	AuthController
}

/**
 * @api {post} /api/v1/system/login 登录
 * @apiGroup system
 * @apiName Login
 * @apiDescription 注释：登录成功会在Header中返回Token字段，后面接口访问会验证请求Header中的Token字段
 * @apiVersion 1.0.0
 * @apiParamExample {json} Request-Example:
 * {
 *   "userID": "wyd", //用户ID
 *   "password": "wyd666" //用户密码
 * }
 * @apiSuccessExample {json} Success-Response:
 * {
 *   "errCode": 200,
 *   "errMsg": "OK"
 * }
 */
func (this *SystemController) Login() {
	replyData, token := models.Login(this.Ctx.Input.RequestBody, this.Ctx.Request.RemoteAddr)
	this.Data["json"] = replyData

	if token != "" {
		//Header中设置Token，允许此Token字段被前端获取
		//this.Ctx.Output.Header("Access-Control-Expose-Headers", "Token")
		this.Ctx.Output.Header("Token", token)
	}

	this.ServeJSON()
}

/**
 * @api {get} /api/v1/system/logout 登出
 * @apiGroup system
 * @apiName Logout
 * @apiVersion 1.0.0
 * @apiSuccessExample {json} Success-Response:
 * {
 *   "errCode": 200,
 *   "errMsg": "OK"
 * }
 */
func (this *SystemController) Logout() {
	token := this.Ctx.Request.Header.Get("Token")

	this.Data["json"] = models.Logout(token, this.Ctx.Request.RemoteAddr)
	this.ServeJSON()
}

/**
 * @api {get} /api/v1/system/info 获取系统信息
 * @apiVersion 1.0.0
 * @apiGroup system
 * @apiName SystemInfo
 * @apiSuccessExample  {json} Response-Example
 * {
 *   "errCode": 200,
 *   "errMsg": "OK",
 *   "cpuUsedPercent": "6%", //CPU使用率
 *   "virtualMemory": {
 *     "total": "8079MB", //总内存
 *     "available": "2565MB", //当前可用内存
 *     "used": "5514MB", //当前已使用内存
 *     "usedPercent": "68%" //当前内存使用率
 *   },
 *   "network": {
 *     "uploadSpeed": "0KB/s", //上传速度
 *     "downloadSpeed": "0KB/s" //下载速度
 *   },
 *   "deviceInfo": {
 *     "totalCount": 0, //设备总数
 *     "onlineCount": 0 //设备在线总数
 *   },
 *   "channelInfo": {
 *     "totalCount": 0, //通道总数
 *     "onlineCount": 0 //通道在线总数
 *   }
 * }
 */
func (this *SystemController) SystemInfo() {
	this.Data["json"] = models.SystemInfo()
	this.ServeJSON()
}
